package org.example.thread;

import java.util.concurrent.TimeUnit;

/*
 *Author:ZouHeng
 *Des:测试Thread.join()方法
 *Date:2021-01-04  14:03
 */
public class Join {
    public static void main(String[] args) {
        Thread previous = Thread.currentThread();
        System.out.println(previous.getName());
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Domino(previous), String.valueOf(i));
            thread.start();
            previous = thread;
        }
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " terminate.");
    }

    public static class Domino implements Runnable {
        private Thread thread;

        public Domino(Thread thread) {
            this.thread = thread;
        }

        @Override
        public void run() {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " terminate.");
        }
    }
}
